import { BTNode } from "../BTNode";
import { BTState } from "../BTState";

/**
 * [行为树]等待节点
 * 等待x秒后返回succ
 */
export class BTWait<T = any> extends BTNode<T> {

    private _endTime: number;

    constructor(
        /** 等待时间(秒) */
        public waitTime: number | ((ctx: T) => number),
        /** 获取当前时间(秒) */
        public getTime?: (ctx: T) => number,
    ) {
        super();
        this.getTime = this.getTime || this._getTime;
    }

    protected onStart(ctx: T): void {
        super.onStart(ctx);
        const waitTime = typeof this.waitTime === "number" ? this.waitTime : this.waitTime(ctx);
        this._endTime = this.getTime(ctx) + waitTime;
    }

    process(ctx: T): BTState {
        if (this.getTime(ctx) >= this._endTime) {
            return BTState.succ;
        }
        return BTState.run;
    }

    protected _getTime() {
        return this.tree.runTime;
    }

}